﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DC2010.Objects;
using DC2010.Effects;
using System.Drawing.Drawing2D;
using DC2010_Editor.Properties;
using DC2010.Tools;
using DC2010;

namespace DC2010_Editor.Formulars
{
    public partial class MasteryGainList : Form
    {
        private List<MasteryGain> lse = null;
        private MasteryGain data { get; set; }

        public MasteryGainList(List<MasteryGain> lse)
        {
            InitializeComponent();

            this.lse = lse;

            //list all masteries
            comboBox1.Items.Add("MASTERY_WIZARD");
            comboBox1.Items.Add("MASTERY_FIGHTER");
            comboBox1.Items.Add("MASTERY_PRIEST");
            comboBox1.Items.Add("MASTERY_NINJA");
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //_lse[e.RowIndex]
        }

        private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
        {
            //Mastery ids
            string ef = (string)comboBox1.Items[e.Index];
            Font font = new Font("Tahoma", 7, FontStyle.Regular);
            Font fontBold = new Font("Tahoma", 8, FontStyle.Bold);
            Color clr1 = Color.White;
            Color clr2 = Color.White;

            clr1 = Color.FromArgb(100,100,200);
            clr2 = Color.LightBlue;

            LinearGradientBrush lgb = new LinearGradientBrush(e.Bounds, clr1, clr2, 90f);

            e.Graphics.FillRectangle(lgb, e.Bounds);
            e.Graphics.DrawRectangle(new Pen(Color.FromArgb(200, 200, 250)), e.Bounds);

            Rectangle r = new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height);
            r.X = r.Width - 25;
            r.Width = 25;
            e.Graphics.DrawRectangle(new Pen(Color.FromArgb(200, 200, 250)), r);


            TextRenderer.DrawText(e.Graphics, ef, font, new Point(e.Bounds.X, e.Bounds.Y + 2), Color.Black);

        }

        private void comboBox1_MeasureItem(object sender, MeasureItemEventArgs e)
        {
            e.ItemHeight = 16;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedItem != null)
            {
                string ef = (string)comboBox1.SelectedItem;

                if (ef != null) 
                    crystalButton1.Enabled = true;
                else
                    crystalButton1.Enabled = false;
            }
            else crystalButton1.Enabled = false;
        }

        private void crystalButton1_Click(object sender, EventArgs e)
        {
            if (comboBox1.SelectedItem != null)
            {
                string ef = (string)comboBox1.SelectedItem;

                MasteryGain sef = new MasteryGain();
                sef.MasteryId = ef;
                sef.Id = ef;
                sef.ExpGain = 0;
                lse.Add(sef);

                RefreshSefList();
            }
        }

        public void RefreshSefList()
        {
            if (imageListBox1.Items != null)
            {
                imageListBox1.Items.Clear();

                if (lse != null)
                {

                    foreach (MasteryGain dp in lse)
                    {
                        Image i = Resources.lightning; //default

                        ImageListBoxItem itm = new ImageListBoxItem(string.Format("{0} ({1})", dp.Id,dp.ExpGain), i, dp);
                        imageListBox1.Items.Add(itm);
                    }
                }
            }

            //RefreshParameters();
        }

        private void SimpleEffectList_Load(object sender, EventArgs e)
        {
            RefreshSefList();
        }

        private void SetControls()
        { 
            // ?? data.Id
            if (data != null)
            {
                textBox1.Text = data.MasteryId;
                textBox2.Text = data.ExpGain.ToString();
            }
        }

        private void GetControls()
        {
            // ?? data.Id
            data.ExpGain = Int32.Parse(textBox2.Text);
            data.MasteryId = textBox1.Text;
        }

        private void imageListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (imageListBox1.SelectedItem != null)
            {
                if (imageListBox1.SelectedItem is ImageListBoxItem)
                {
                    ImageListBoxItem we = (ImageListBoxItem)imageListBox1.SelectedItem;
                    data = (MasteryGain)we.Tag;

                    SetControls();
                    //RefreshParameters();
                }
            }
        }

        private void crystalButton3_Click(object sender, EventArgs e)
        {
            GetControls();
        }

        private void crystalButton2_Click(object sender, EventArgs e)
        {
            if (imageListBox1.SelectedItem != null)
            {
                if (imageListBox1.SelectedItem is ImageListBoxItem)
                {
                    ImageListBoxItem we = (ImageListBoxItem)imageListBox1.SelectedItem;
                    MasteryGain sef = (MasteryGain)we.Tag; ;

                    lse.Remove(sef);
                }
            }

            RefreshSefList();
        }
    }
}
